#include "singlemainview.h"
#include <QListWidget>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QStandardItemModel>
#include "simpleutil.h"
#include "shellmanager.h"
#include <QScrollArea>
#include <QDebug>
#include <QModelIndex>


#include "filereversethread.h"

void SingleMainView::setRootAndWorkDir(QString rootdir, QString workdir)
{
    _workdir = workdir;
    _rootdir = rootdir;
    sendRootFilePath(_rootdir);
    sendRootFilePath(_workdir);
}

void SingleMainView::reveiveRootFilePath(QString file){
     QListWidgetItem *dropbox_item = new QListWidgetItem();
     dropbox_item->setIcon(QIcon(":/res/png/stylesheet/open.jpg"));
     dropbox_item->setText(file);
     dropbox_item->setSizeHint(QSize(60, 22));
     _lviewroot->addItem(dropbox_item);
}

void SingleMainView::reveiveWorkFilePath(QString file){
     QListWidgetItem *dropbox_item = new QListWidgetItem();
     dropbox_item->setIcon(QIcon(":/res/png/stylesheet/open.jpg"));
     dropbox_item->setText(file);
     dropbox_item->setSizeHint(QSize(60, 22));
     _lviewroot->addItem(dropbox_item);
}

void SingleMainView::initSingnalSlot(){
    _reverseRootThread = new FileReverseThread();
    _reverseWorkThread = new FileReverseThread();

    connect(_reverseRootThread, SIGNAL(sendFilePath(QString)), this, SLOT(reveiveRootFilePath(QString)));
    connect(this, SIGNAL(sendRootFilePath(QString)), _reverseRootThread, SLOT(receiveFilePath(QString)));

    connect(_reverseWorkThread, SIGNAL(sendFilePath(QString)), this, SLOT(reveiveWorkFilePath(QString)));
    connect(this, SIGNAL(sendWorkFilePath(QString)), _reverseWorkThread, SLOT(receiveFilePath(QString)));

    _reverseRootThread->start();
    _reverseWorkThread->start();

    connect(_lviewroot,SIGNAL(clicked(QModelIndex)),this,SLOT(listClick(QModelIndex)));
}

void SingleMainView::initMainView(){
    QHBoxLayout* hbox = new QHBoxLayout();
    _lviewroot = new QListWidget();
    _sutil = new SimpleUtil;
    _smanager = new shellmanager;

    ///遍历文件夹
    //initRootListView();

    hbox->addWidget(_lviewroot);
    QScrollArea* scroll = new QScrollArea;
    scroll->setContentsMargins(0,0,0,0);

    ///初始化一个widget用来存放Gridlayout
    QWidget *pWidget = new QWidget();
    pWidget->setLayout(hbox);

    int width = _lviewroot->width();
    int height = _lviewroot->height();

    qDebug() << "width:" << width << " height:" << height;

    ///将widget插入到scrolllayout中
    scroll->setWidget(pWidget);
    scroll->setWidgetResizable(true);

    ///初始化一个vboxlayout,将scrolllayout放进去
    QVBoxLayout *mainLayout = new QVBoxLayout();
    mainLayout->addWidget(scroll);
    mainLayout->setContentsMargins(0,0,0,0);

    //vbox->addLayout(hbox);
    //setLayout(vbox);
    setLayout(mainLayout);

    ///设置window样式
    setWindowState(Qt::WindowMaximized);
    ///无边框
    ///setWindowFlags(Qt::FramelessWindowHint);
    ///背景透明
    setAttribute(Qt::WA_TranslucentBackground);

    initSingnalSlot();
}

SingleMainView::SingleMainView(QWidget *parent) : QWidget(parent)
{
    initMainView();
}

void SingleMainView::listClick(QModelIndex index){
    QString strTemp, strNextFile;
    strTemp = index.data().toString();
    strNextFile = index.siblingAtRow(1).data().toString();

    //QMessageBox msg;
    //msg.setText(strTemp);
    //msg.exec();

    QStringList arguments;
    arguments.append("/silent");
    arguments.append(strTemp);
    //QDir::currentPath();
    arguments.append(strNextFile);
    //dir E:\BeyondCompare\BCompare.exe   /fv="Folder Compare" "X:\AHK\框架试验\框架\gui框架4.0"  "X:\AHK\框架试验\框架\gui框架3.0"
    _smanager->startProgram("D:\\Program Files\\Beyond Compare 4\\BCompare.exe", arguments);
}


void SingleMainView::initRootListView(){
    _itemmodelroot = new QStandardItemModel(this);
    QStringList strList;
    ///遍历文件夹
    strList = _sutil->reverseDir("D:\\WorkStation\\HolyCompare");

    int nCount = strList.size();
    for(int i = 0; i < nCount; i++)
    {
       QString string = static_cast<QString>(strList.at(i));
//       QStandardItem *item = new QStandardItem(string);
//       _itemmodelroot->appendRow(item);

        QListWidgetItem *dropbox_item = new QListWidgetItem();
        dropbox_item->setIcon(QIcon(":/res/png/stylesheet/open.jpg"));
        dropbox_item->setText(string);
        dropbox_item->setSizeHint(QSize(60, 22));
        _lviewroot->addItem(dropbox_item);
    }
    //_lviewroot->setModel(_itemmodelroot);
    //_lviewroot->setFixedSize(200,300);

    _rootcols = nCount;

    connect(_lviewroot,SIGNAL(clicked(QModelIndex)),this,SLOT(listClick(QModelIndex)));
}
